package cn.zhyjohn.paysystem.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.FileSystems;
import java.nio.file.Path;

/**
 * @author ZHY
 * @since 2021/7/1 12:21
 */
public class QRCodeUtils {
    public static final Logger logger = LoggerFactory.getLogger(QRCodeUtils.class);

    public static void createQRCode(String content,Integer weight,Integer height,String path){
        try {
            QRCodeWriter codeWriter = new QRCodeWriter();
            BitMatrix matrix = codeWriter.encode(content, BarcodeFormat.QR_CODE, height, weight);
            Path paths = FileSystems.getDefault().getPath(path);
            MatrixToImageWriter.writeToPath(matrix, "PNG", paths);
        }catch (Exception e){
            logger.error("生成二维码错误，error:{}",e.getMessage(),e);
        }
    }

    public static void main(String[] args) {
        createQRCode("dddd",1,1,"D://demo//1.png");
    }
}
